package com.synjones.zeuscloudweb.controller;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.synjones.zeus.model.authorize.AuthorizeInfo;
import com.synjones.zeus.model.web.LisenceBean;
import com.synjones.zeus.model.ykt.pay.PayRecoreInfo;
import com.synjones.zeuscloudweb.service.AuthorizeService;
import com.synjones.zeuscloudweb.util.ccl.datastructure.impl.BaseDto;
import com.synjones.zeuscloudweb.util.ccl.util.G4Utils;
import net.sf.json.JSONObject;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;


/**
 * @author Think
 * Description: 授权管理
 * @date created in 22:40 2018-05-21
 * Modified by:
 */
@Controller
@RequestMapping("/authorize")
public class AuthorizeController {

    @Autowired
    @Qualifier("authorizeServiceImpl")
    private AuthorizeService authorizeService;

    /**
     *  授权设备列表跳转
     *  @author Think
     *  Description: 授权设备列表跳转
     *  @date 22:40 2018-04-24
     *  @return String
     */
    @GetMapping(value = "/authorizeListInfo")
    public String authorizeListInfo(){
        return "authorize-list";
    }

    /**
     *  lisence列表跳转
     *  @author Think
     *  Description: lisence列表跳转
     *  @date 00:40 2018-05-26
     *  @return String
     */
    @GetMapping(value = "/lisenceListInfo")
    public String lisenceListInfo(){
        return "lisence-list";
    }

    /**
     * 添加lisence页面跳转
     * @param
     * @author Think
     * description:  添加lisence页面跳转
     * @date  01:29 2018-05-26
     * return:
     */
    @GetMapping(value = "/addLisenceInfo")
    public String addLisenceInfo() {
        return "lisence-add";
    }


    /**
     *  获取授权设备列表信息分页展示
     *  @param
     *  @author Think
     *  Description: 获取授权设备列表信息分页展示
     *  @date 22:40 2018-05-21
     *  @return
     *  throws
     */
    @PostMapping(value = "/getAuthorizeListByPage")
    @ResponseBody
    public Map getAuthorizeListByPage(HttpServletRequest request, AuthorizeInfo authorizeInfo) {
        //获取排序字段
        String orderColumn = request.getParameter("orderColumn");
        if (orderColumn == null) {
            orderColumn = "id";
        }
        //获取排序方式
        String orderDir = request.getParameter("orderDir");
        if (orderDir == null) {
            orderDir = "asc";
        }
        //直接返回前台
        String draw = request.getParameter("draw");
        //数据起始位置
        String startIndex = request.getParameter("startIndex");
        //每页显示的条数
        String pageSize = request.getParameter("pageSize");

        //获取查询字段
        String orgCode = request.getParameter("orgCode");
        String startTime = request.getParameter("startTime");
        String endTime = request.getParameter("endTime");
        //分页处理，显示第一页的10条数据
        Page<PayRecoreInfo> page = PageHelper.offsetPage((Integer.parseInt(startIndex)), (Integer.parseInt(pageSize)));
        authorizeService.getAuthorizeListByPage(startTime, endTime, orderColumn, orderDir, authorizeInfo);
        PageInfo<PayRecoreInfo> pageInfo = new PageInfo<>(page);
        Map<Object, Object> info = new HashMap<>();
        info.put("pageData", pageInfo.getList());
        info.put("total", pageInfo.getTotal());
        info.put("draw", draw);
        return info;
    }

    /**
     *  获取lisence列表信息分页展示
     *  @param
     *  @author Think
     *  Description: 获取lisence列表信息分页展示
     *  @date 00:40 2018-05-26
     *  @return
     *  throws
     */
    @PostMapping(value = "/getLisenceListByPage")
    @ResponseBody
    public Map getLisenceListByPage(HttpServletRequest request, AuthorizeInfo authorizeInfo){
        //获取排序字段
        String orderColumn = request.getParameter("orderColumn");
        if (orderColumn == null) {
            orderColumn = "id";
        }
        //获取排序方式
        String orderDir = request.getParameter("orderDir");
        if (orderDir == null) {
            orderDir = "asc";
        }
        //直接返回前台
        String draw = request.getParameter("draw");
        //数据起始位置
        String startIndex = request.getParameter("startIndex");
        //每页显示的条数
        String pageSize = request.getParameter("pageSize");
        //获取查询字段
        String orgCode = request.getParameter("orgCode");
        String startTime = request.getParameter("startTime");
        String endTime = request.getParameter("endTime");
        if (null != orgCode && !"".equals(orgCode)) {
            authorizeInfo.setOrgCode(orgCode);
        }
        //分页处理，显示第一页的10条数据
        Page<PayRecoreInfo> page = PageHelper.offsetPage((Integer.parseInt(startIndex)), (Integer.parseInt(pageSize)));
        authorizeService.getLisenceListByPage(startTime, endTime, orderColumn, orderDir, authorizeInfo);
        PageInfo<PayRecoreInfo> pageInfo = new PageInfo<>(page);
        Map<Object, Object> info = new HashMap<>();
        info.put("pageData", pageInfo.getList());
        info.put("total", pageInfo.getTotal());
        info.put("draw", draw);
        return info;
    }

    /**
     *  批量删除授权信息
     *  @param imei 角色ID集合
     *  @author Think
     *  Description:  批量删除角色信息
     *  @date  14:54 2018-05-25
     *  @return
     *  throws
     */
    @PostMapping(value = "/deleteAuthorizeByImei")
    public void deleteAuthorizeByImei(@Param("imei")String imei , HttpServletResponse response) throws IOException {
        PrintWriter pw = response.getWriter();
        BaseDto jsonDao;
        if(G4Utils.isNotEmpty(imei)){
            jsonDao = authorizeService.deleteAuthorizeByImei(imei);
        }else{
            jsonDao = new BaseDto();
            jsonDao.put("success",new Boolean(false));
            jsonDao.put("msg","请求参数出错! 请重新选择");
            jsonDao.put("data",null);
        }
        pw.write(JSONObject.fromObject(jsonDao).toString());
        pw.close();
    }

    /**
     *  批量删除lisence信息
     *  @param id ID集合
     *  @author Think
     *  Description:  批量删除lisence信息
     *  @date  14:54 2018-05-25
     *  @return
     *  throws
     */
    @PostMapping(value = "/deleteLisenceById")
    public void deleteLisenceById(@Param("id")String id , HttpServletResponse response) throws IOException {
        PrintWriter pw = response.getWriter();
        BaseDto jsonDao;
        if(G4Utils.isNotEmpty(id)){
            jsonDao = authorizeService.deleteLisenceById(id);
        }else{
            jsonDao = new BaseDto();
            jsonDao.put("success",new Boolean(false));
            jsonDao.put("msg","请求参数出错! 请重新选择");
            jsonDao.put("data",null);
        }
        pw.write(JSONObject.fromObject(jsonDao).toString());
        pw.close();
    }

    /**
     * 添加许可
     * @param lisenceBean 许可实体
     * @return BaseDto
     * @author Think
     * Description: 添加许可
     * @date 10:27 2018-05-26
     */
    @RequestMapping(value = "/addLisence", method = RequestMethod.POST)
    @ResponseBody
    public BaseDto addLisence(LisenceBean lisenceBean) {
        return authorizeService.insertLisence(lisenceBean);
    }

    /**
     *  修改许可页面跳转
     *  @param id id
     *  @author yys
     *  Description:  修改许可页面跳转
     *  @date  21:16 2018-05-16
     *  @return Model
     */
    @RequestMapping(value = "/updateLisenceInfo" , method = RequestMethod.GET)
    public String updateLisenceInfo(Model model, @Param("id")String  id){
        LisenceBean lisenceBean = authorizeService.selectLisenceById(id);
        model.addAttribute("lisenceBean",lisenceBean);
        return "lisence-update";
    }

    /**
     *  修改许可
     *  @param lisenceBean
     *  @author Think
     *  Description: 修改许可
     *  @date 21:16 2018-05-16
     *  @return BaseDto
     */
    @RequestMapping(value = "/updateLisence" , method = RequestMethod.POST)
    @ResponseBody
    public BaseDto updateLisence(LisenceBean lisenceBean){
        return authorizeService.updateLisence(lisenceBean);
    }
}
